package com.hxy.subject.application.controller;

import com.alibaba.fastjson.JSON;
import com.hxy.subject.application.convert.SubjectCategoryDTOConverter;
import com.hxy.subject.application.dto.AddSubjectCategoryDTO;
import com.hxy.subject.application.dto.DelSubjectCategoryDTO;
import com.hxy.subject.application.dto.EditSubjectCategoryDTO;
import com.hxy.subject.application.dto.SubjectCategoryDTO;
import com.hxy.subject.application.vo.SubjectCategoryVO;
import com.hxy.subject.common.entity.Result;
import com.hxy.subject.domain.entity.SubjectCategoryBO;
import com.hxy.subject.domain.service.ISubjectCategoryDomainService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author haoxuyang
 * @Date 2024/9/2 18:51
 */
@Slf4j
@RestController
@RequestMapping("/subject/category")
public class SubjectCategoryController {

    @Resource
    private ISubjectCategoryDomainService subjectCategoryDomainService;

    @PostMapping("/add")
    public Result<String> add(@RequestBody @Validated AddSubjectCategoryDTO dto) {
        log.info("dto:" + JSON.toJSONString(dto));
        SubjectCategoryBO subjectCategoryBO = SubjectCategoryDTOConverter.INSTANCE.subjectCategoryDtoToBo(dto);
        subjectCategoryDomainService.add(subjectCategoryBO);
        return Result.success("操作成功");
    }

    @PostMapping("/update")
    public Result<String> update(@RequestBody @Validated EditSubjectCategoryDTO dto) {
        SubjectCategoryBO subjectCategoryBO = SubjectCategoryDTOConverter.INSTANCE.subjectCategoryDtoToBo(dto);
        subjectCategoryDomainService.update(subjectCategoryBO);
        return Result.success("操作成功");
    }

    @PostMapping("/delete")
    public Result<String> delete(@RequestBody @Validated DelSubjectCategoryDTO dto) {
        SubjectCategoryBO subjectCategoryBO = SubjectCategoryDTOConverter.INSTANCE.subjectCategoryDtoToBo(dto);
        subjectCategoryDomainService.delete(subjectCategoryBO);
        return Result.success("操作成功");
    }


    /**
     * 查询分类大类
     *
     * @return
     */
    @PostMapping("/queryPrimaryCategory")
    public Result<List<SubjectCategoryVO>> queryPrimaryCategory(@RequestBody SubjectCategoryDTO dto) {
        SubjectCategoryBO bo = SubjectCategoryDTOConverter.INSTANCE.subjectCategoryDtoToBo(dto);
        List<SubjectCategoryBO> subjectCategoryBOList = subjectCategoryDomainService.queryPrimaryCategory(bo);
        List<SubjectCategoryVO> subjectCategoryVOList = SubjectCategoryDTOConverter.INSTANCE.subjectCategoryBoToVoList(subjectCategoryBOList);
        return Result.success("查询成功", subjectCategoryVOList);
    }

    /**
     * 根据分类id查询小类
     *
     * @return
     */
    @PostMapping("/queryCategoryByPrimary")
    public Result<List<SubjectCategoryVO>> queryCategoryByPrimary(@RequestBody SubjectCategoryDTO dto) {
        SubjectCategoryBO bo = SubjectCategoryDTOConverter.INSTANCE.subjectCategoryDtoToBo(dto);
        List<SubjectCategoryBO> subjectCategoryBOList = subjectCategoryDomainService.queryCategoryByPrimary(bo);
        List<SubjectCategoryVO> subjectCategoryVOList = SubjectCategoryDTOConverter.INSTANCE.subjectCategoryBoToVoList(subjectCategoryBOList);
        return Result.success("查询成功", subjectCategoryVOList);
    }

}
